package com.it.nongchanping.controller;

import com.it.nongchanping.service.NewsService;
import com.it.nongchanping.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * 首页控制器
 */
@Controller
public class HomeController {
    
    @Autowired
    private NewsService newsService;
    
    @Autowired
    private ProductService productService;
    
    /**
     * 首页
     */
    @GetMapping("/")
    public String index(Model model) {
        try {
            // 获取最新新闻（扶贫政策、农产品文化故事、助农活动新闻）
            model.addAttribute("policyNews", newsService.getNewsByType("扶贫政策"));
            model.addAttribute("cultureNews", newsService.getNewsByType("农产品文化故事"));
            model.addAttribute("activityNews", newsService.getNewsByType("助农活动新闻"));
            
            // 获取推荐农产品
            model.addAttribute("recommendProducts", productService.getAvailableProducts());
        } catch (Exception e) {
            // 如果数据库有问题，提供空列表
            e.printStackTrace();
            model.addAttribute("policyNews", java.util.List.of());
            model.addAttribute("cultureNews", java.util.List.of());
            model.addAttribute("activityNews", java.util.List.of());
            model.addAttribute("recommendProducts", java.util.List.of());
        }
        
        return "index";
    }
    
    /**
     * 首页（重定向）
     */
    @GetMapping("/index")
    public String home() {
        return "redirect:/";
    }
} 